package com.dataeco.api.service.impl;

import com.dataeco.api.entity.BankAccount;
import com.dataeco.api.exception.UnknownResourceException;
import com.dataeco.api.repository.BankAccountRepository;
import com.dataeco.api.service.BankAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


@Service
@Transactional
public class BankAccountServiceImpl extends BaseServiceImpl implements BankAccountService{

    private final BankAccountRepository bankAccountRepository;

    @Autowired
    public BankAccountServiceImpl(BankAccountRepository bankAccountRepository) {
        this.bankAccountRepository = bankAccountRepository;
    }

    @Override
    public List<BankAccount> findAll() {
        String userId=getUserId();
        if (Objects.isNull(userId)){
            throw new UnknownResourceException("userId is null");
        }
        return bankAccountRepository.findByUserId(userId);
    }

    @Override
    public BankAccount saveBankAccount(BankAccount bankAccount) {
        if (Objects.isNull(bankAccount.getUser())){
            bankAccount.setUser(getUser());
        }
        return bankAccountRepository.save(bankAccount);
    }
}
